<template>
  <editor-layout @ok="handleOk" v-on="$listeners" v-loading="loading">
    <el-form :model="dataForm" :rules="dataRule" ref="form" @keyup.enter.native="handleOk()" label-width="80px">
      <el-row :gutter="20" class="flex-row">
        <el-col :span="12">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="dataForm.username" placeholder="客户姓名" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="dataForm.mobile" placeholder="手机号(用户登录账号)" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密码" prop="password" :class="{ 'is-required': !dataForm.id }">
            <el-input v-model="dataForm.password" type="password" placeholder="密码" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="确认密码" prop="comfirmPassword" :class="{ 'is-required': !dataForm.id }">
            <el-input v-model="dataForm.comfirmPassword" type="password" placeholder="确认密码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="dataForm.email" placeholder="邮箱" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别" size="mini" prop="sex">
            <my-radio v-model="dataForm.sex" :options="sex">
            </my-radio>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="信用代码" prop="billing">
            <el-input v-model="dataForm.billing" placeholder="信用代码/税号" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址" prop="address">
            <el-input v-model="dataForm.address" placeholder="经营场所地址" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开户行" prop="bank">
            <el-input v-model="dataForm.bank" placeholder="开户行" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="银行卡号" prop="bankCard">
            <el-input v-model="dataForm.bankCard" placeholder="银行卡号" clearable></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="状态" size="mini" prop="status">
            <el-radio-group v-model="dataForm.status">
              <el-radio label="0">禁用</el-radio>
              <el-radio label="1">正常</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
  </editor-layout>
</template>

<script>
import { mapState } from 'vuex'
import { isEmail, isMobile } from '@/utils/validate'
import {getSysTuserInfoUserId, postSysTuserSave, postSysTuserUpdate} from '$my/code/api/t-user'

export default {
  name: 'edit',
  props: {
    id: [String, Number]
  },
  data() {
    const validatePassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    const validateComfirmPassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('确认密码不能为空'))
      } else if (this.dataForm.password !== value) {
        callback(new Error('确认密码与密码输入不一致'))
      } else {
        callback()
      }
    }
    const validateEmail = (rule, value, callback) => {
      if(!value) {
        callback()
      } else if (!isEmail(value)) {
        callback(new Error('邮箱格式错误'))
      } else {
        callback()
      }
    }
    const validateMobile = (rule, value, callback) => {
      if (!isMobile(value)) {
        callback(new Error('手机号格式错误'))
      } else {
        callback()
      }
    }
    return {
      loading: true,
      roleList: [],
      tempPassword: '',
      dataForm: {
        userId: 0,
        username: '',
        password: '',
        comfirmPassword: '',
        email: '',
        mobile: '',
        status: '1',
        sex: '',
        billing: '',
        address: '',
        bank: '',
        bankCard: ''
      },
      dataRule: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { validator: validatePassword, trigger: 'blur' }
        ],
        comfirmPassword: [
          { validator: validateComfirmPassword, trigger: 'blur' }
        ],
        email: [
          { validator: validateEmail, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async init() {
      this.loading = true

      if(this.id) {
        const data = await getSysTuserInfoUserId(this.id)
        this.tempPassword = data.password
        this.dataForm = {
          ...data,
          comfirmPassword: this.tempPassword
        }
      }
      this.loading = false
    },
    handleOk() {
      this.$refs.form.validate(ok => {
        if(!ok) return

        let saveFn = postSysTuserSave
        if(this.id) {
          saveFn = postSysTuserUpdate
        }
        this.loading = true
        const params = {...this.dataForm}
        if(this.tempPassword === this.dataForm.password) {
          // 加密密码没有被修改
          delete params.password
        } else {
          delete params.comfirmPassword
        }

        saveFn(params).then(res => {
          this.loading = false
          this.$notify.success('操作成功！')
          this.$emit('on-ok')
        })

      })
    }
  },
  computed: {
    ...mapState('constant', ['sex'])
  },
  created() {
    this.init()
  }
}
</script>

<style lang="scss" scoped>
.user-edit {
}
</style>
